package jmine.tec.aom.services.include;

import jmine.tec.aom.AOMMessages;
import jmine.tec.aom.dao.PropertyTypeDAO;
import jmine.tec.aom.domain.EntityProperty;
import jmine.tec.aom.domain.PropertyType;
import jmine.tec.aom.domain.impl.BasePropertyTypes;
import jmine.tec.di.annotation.Injected;
import jmine.tec.services.annotations.ServiceImplementor;
import jmine.tec.services.api.ActionsEnum;
import jmine.tec.services.api.RawInput;
import jmine.tec.services.api.ServiceValidationExecutionException;
import jmine.tec.services.api.export.ReferenceMap;
import jmine.tec.services.export.impl.ExportUtils;
import jmine.tec.services.io.ServiceBean;

/**
 * Service for creating {@link EntityProperty}
 * 
 * @author jrenaut
 */
@ServiceImplementor(action = ActionsEnum.INCLUIR)
public class EntityPropertyService extends AbstractPropertyService<EntityProperty> {

    public static final String ENTITY_TYPE = "Entity type";

    private PropertyTypeDAO ptDAO;

    private RawInput props;

    /**
     * {@inheritDoc}
     */
    @Override
    protected void fillSpecificServiceBean(ServiceBean bean, ReferenceMap referenceMap, EntityProperty entity) {
        ExportUtils.put(bean, ENTITY_TYPE, entity.getEntityType());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void executeSpecific(EntityProperty property) {
        super.executeSpecific(property);

        String entityClass = this.props.getRaw(ENTITY_TYPE);
        if (entityClass == null || entityClass.trim().length() == 0) {
            throw new ServiceValidationExecutionException(AOMMessages.FIELD_REQUIRED.create(ENTITY_TYPE));
        }
        property.setEntityType(entityClass);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected PropertyType getPropertyType() {
        return this.ptDAO.findBySystemIdentifier(BasePropertyTypes.ENTITY);
    }

    /**
     * @param ptDAO the ptDAO to set
     */
    @Injected
    public void setPtDAO(PropertyTypeDAO ptDAO) {
        this.ptDAO = ptDAO;
    }

    /**
     * @param props the props to set
     */
    @Injected
    public void setProps(RawInput props) {
        this.props = props;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected Class<EntityProperty> getPropertyClass() {
        return EntityProperty.class;
    }

}
